export default {

    //清理背景
    clearCanvas: function (gl, color, a = 1) {
        gl.clearColor(color.r, color.g, color.b, a);
        gl.clear(gl.COLOR_BUFFER_BIT);
    },
    clearCanvas: function (gl, [r, g, b, a]) {
        gl.clearColor(r, g, b, a);
        gl.clear(gl.COLOR_BUFFER_BIT);
    },
    cross: function ([p0, p1, p2]) {
        const [ax, ay, bx, by] = [
            p1.x - p0.x,
            p1.y - p0.y,
            p2.x - p0.x,
            p2.y - p0.y,
        ];
        return ax * by - bx * ay;
    },
    //判断点是否在三角形中,需要三个点逆时针旋转
    //如果是顺时针旋转的画, 结果需要都<=0即可判断(也就是完全相反)
    inTriangle: function (p, nodeArr) {
        let tempArr = [
            this.cross([nodeArr[0], nodeArr[1], p]),
            this.cross([nodeArr[1], nodeArr[2], p]),
            this.cross([nodeArr[2], nodeArr[0], p]),
        ];
        // console.log(tempArr)
        if (tempArr[0] >= 0 &&
            tempArr[1] >= 0 &&
            tempArr[2] >= 0)
            return true;
        return false;
    },
    //同样是逆时针旋转
    //前几步都一样, 求出两个向量后用叉乘求出法线
    //这个法线是有方向的, 如果该法线与平面法线相同 则dot>0 即在同一平面
    //如果 法线 与 平面法线 的点积都>0 则点在三角形中
    inTriangle3: function (m, nodeArr) {
        let retBool = true;
        const n = nodeArr[0].clone().sub(nodeArr[1])
            .cross(nodeArr[1].clone().sub(nodeArr[2]));
        for (let i = 0; i < 3; i++) {
            const j = (i + 1) % 3;
            const [a, b] = [nodeArr[i], nodeArr[j]]
            const ma = a.clone().sub(m);
            const ab = b.clone().sub(a);
            const d = ma.clone().cross(ab);
            const len = d.dot(n); //为什么需要这一步???明明已经求出了在同一平面的点 直接用2D的不就OK了
            if (len < 0) {
                retBool = false;
                break;
            }
            // console.log(len)
        }
        return retBool;
    },
    //屏幕坐标转化Canvs坐标:X
    GetPosiOfX: function (canvasW, cssX) {
        return (cssX - canvasW / 2) / (canvasW / 2);
    },
    //屏幕坐标转化Canvs坐标:Y
    GetPosiOfY: function (canvasH, cssY) {
        return (-1 * (cssY - canvasH / 2)) / (canvasH / 2);
    },
    GetCanvasPosi: function (event, canvas) {
        const { clientX, clientY } = event;
        const { left, top, width, height } = canvas.getBoundingClientRect();
        const [cssX, cssY] = [clientX - left, clientY - top];
        // console.log("clientX:" + clientX, "clientY" + clientY)
        return { x: this.GetPosiOfX(width, cssX), y: this.GetPosiOfY(height, cssY) };
    },
    GetCanvasCss: function (clientX, clientY, canvas) {
        const { left, top, width, height } = canvas.getBoundingClientRect();
        var [x, y] = [0, 0];
        x = clientX * (width / 2) + (width / 2);
        y = clientY * (height / 2) * -1 + (height / 2);
        return { x: x + left, y: y + top };
    },
    //取颜色中间值
    gradientColor: function (startColor = [], endColor = [], proportion = 1.0) {

        // 此处调用了 自定义的 16 进制转换 10 进制的函数
        const startColorDecimalisArray = startColor;

        const endColorDecimalisArray = endColor;

        // 两个颜色的差值数组

        const rgbDifferenceArray = [];

        //endColor 的 rgb 值 分别减掉 startColor 的 rgb 值并分别记录

        for (let index = 0; index < startColorDecimalisArray.length; index++) {

            rgbDifferenceArray.push(endColorDecimalisArray[index] - startColorDecimalisArray[index]);

        }

        // startColor 的 rgb 值 分别加上对应比例的 rgb 差值 得到 结果色值的 rgb 数组

        const resultRgbHexArray = rgbDifferenceArray.map((item, index) => {

            let resultVal;

            resultVal = Math.round(startColorDecimalisArray[index] + item * proportion);

            // 将 10 进制的 值转换为 16 进制

            return resultVal;

        });
        return resultRgbHexArray;
    },
    //将平面中的电转换为线段
    planeNodeToLineOf3D: function (nodes, eachCount) {
        let tempArr = new Array(eachCount);
        for (let i = 0; i < eachCount; i++) {
            tempArr[i] = new Array(eachCount);
        }

        var getIndex = (x, y) => {
            return ((y * eachCount) + x) * 3;
        }

        let retArr = [];
        for (let y = 0; y < eachCount; y++) {
            for (let x = 0; x < eachCount; x++) {
                let [node1, node2, node3, node4] = [getIndex(x, y), getIndex(x + 1, y), getIndex(x + 1, y + 1), getIndex(x, y + 1)];

                if (x == eachCount - 1 && y == eachCount - 1) {

                } else if (x == eachCount - 1) {
                    retArr.push(
                        nodes[node1 + 0],
                        0,
                        nodes[node1 + 2],
                    );
                    retArr.push(
                        nodes[node4 + 0],
                        0,
                        nodes[node4 + 2],
                    );
                } else if (y == eachCount - 1) {
                    retArr.push(
                        nodes[node1 + 0],
                        0,
                        nodes[node1 + 2],
                    );
                    retArr.push(
                        nodes[node2 + 0],
                        0,
                        nodes[node2 + 2],
                    );
                }
                else {
                    retArr.push(
                        nodes[node1 + 0],
                        0,
                        nodes[node1 + 2],
                    );
                    retArr.push(
                        nodes[node2 + 0],
                        0,
                        nodes[node2 + 2],
                    );

                    retArr.push(
                        nodes[node1 + 0],
                        0,
                        nodes[node1 + 2],
                    );
                    retArr.push(
                        nodes[node3 + 0],
                        0,
                        nodes[node3 + 2],
                    );

                    retArr.push(
                        nodes[node1 + 0],
                        0,
                        nodes[node1 + 2],
                    );
                    retArr.push(
                        nodes[node4 + 0],
                        0,
                        nodes[node4 + 2],
                    );
                }



            }
        }
        return retArr;
    },
    //将正方形平面中的电转换为三角形
    planeNodeToTriangleOf3D: function (nodes, eachCount) {
        let tempArr = new Array(eachCount);
        for (let i = 0; i < eachCount; i++) {
            tempArr[i] = new Array(eachCount);
        }

        var getIndex = (x, y) => {
            return ((y * eachCount) + x) * 3;
        }

        let retArr = [];
        for (let y = 0; y < eachCount - 1; y++) {
            for (let x = 0; x < eachCount - 1; x++) {

                //第一个三角形
                let [node1, node2, node3] = [getIndex(x, y), getIndex(x, y + 1), getIndex(x + 1, y + 1)];
                retArr.push(
                    nodes[node1 + 0],
                    0,
                    nodes[node1 + 2],
                );
                retArr.push(
                    nodes[node2 + 0],
                    0,
                    nodes[node2 + 2],
                );
                retArr.push(
                    nodes[node3 + 0],
                    0,
                    nodes[node3 + 2],
                );

                //第二个三角形
                retArr.push(
                    nodes[getIndex(x, y) + 0],
                    0,
                    nodes[getIndex(x, y) + 2],
                );
                retArr.push(
                    nodes[getIndex(x + 1, y) + 0],
                    0,
                    nodes[getIndex(x + 1, y) + 2],
                );
                retArr.push(
                    nodes[getIndex(x + 1, y + 1) + 0],
                    0,
                    nodes[getIndex(x + 1, y + 1) + 2],
                );
            }
        }

        return retArr;
    },
    //获取图片的重新位置
    //glw:一个象限的x数 如0.5
    //sccWH: 图片的 Width / Height
    getImageCentre: function (glw, sccWH) {
        return glw / sccWH * (canvas.width / canvas.height);
    },
    //获取颜色
    getPoxels: function (gl, offsetX, offsetY) {
        if (offsetX == 1)
            offsetX = 0.99999;
        if (offsetY == 1)
            offsetY = 0.99999;
        const pixel = new Uint8Array(4);
        gl.readPixels(
            canvas.width * offsetX, canvas.height * offsetY,
            1, 1,
            gl.RGBA,
            gl.UNSIGNED_BYTE,
            pixel);
        console.log(pixel)
        return pixel;
    }

}
